package rpc

import (
	"context"
	"fmt"
	"net"

	"mpa-dance-push/src/logger"
	pb "mpa-dance-push/src/message"

	"google.golang.org/grpc"
)

const port = ":50001"

type server struct{}

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
	logger.Info.Printf("Received: %v", in.GetMessage())
	return &pb.HelloResponse{Message: "Hello " + in.GetMessage()}, nil
}

// InitRPCServer 初始化 rpc server
func InitRPCServer() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		logger.Error.Println("fail")
		return
	}
	fmt.Printf("[Init Tcp Server: Port => %s \n]", port)
	s := grpc.NewServer()
	pb.RegisterHelloServiceServer(s, &server{})
	if err := s.Serve(lis); err != nil {
		logger.Error.Printf("failed to serve: %v", err)
	}
}

// protoc -I message/ message/hello.proto --go_out=plugins=grpc:message
